package com.yanyilu.qiubite.controller;

import com.yanyilu.qiubite.entity.Spot;
import com.yanyilu.qiubite.entity.Story;
import com.yanyilu.qiubite.service.AdminUserService;
import com.yanyilu.qiubite.service.StoryService;
import com.yanyilu.qiubite.util.PageUtil;
import com.yanyilu.qiubite.util.ParseJwtUtil;
import com.yanyilu.qiubite.util.R;
import com.yanyilu.qiubite.util.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("story")
public class StoryController {
    @Autowired
    StoryService storyService;

    @Autowired
    AdminUserService adminUserService;

    /**
     * 添加故事
     * @param story
     * @return
     */
    @PostMapping("/admin/save")
    public R addStory(@RequestBody Story story){
        String role = ParseJwtUtil.getRole();
        if("1".equals(role)){
            Integer userid = ParseJwtUtil.getUserId();
            Spot spot = adminUserService.findSpotUserId(userid);
            story.setSpotId(spot.getSpotId());
            story.setSpotName(spot.getSpotName());
            storyService.addStory(story);
            return R.ok();
        }else if("0".equals(role)){
            if(null == story.getSpotId()){
                return R.error("请先选择景区！");
            }else{
                storyService.addStory(story);
                return R.ok();
            }
        }else {
            return R.error(ResultCode.NO_ROLE_ERROR);
        }
    }

    /**
     * 修改故事
     * @param story
     * @return
     */
    @PostMapping("/admin/update")
    public R updateStory(@RequestBody Story story){
        String role = ParseJwtUtil.getRole();
        if("1".equals(role)){
            Integer userid = ParseJwtUtil.getUserId();
            Integer spotId = adminUserService.findSpotIdUserId(userid);
            if(spotId.equals(story.getSpotId())){
                storyService.addStory(story);
                return R.ok();
            }else {
                return R.error(ResultCode.NO_ROLE_ERROR);
            }
        }else if("0".equals(role)){
                storyService.addStory(story);
                return R.ok();
        }else {
            return R.error(ResultCode.NO_ROLE_ERROR);
        }
    }

    /**
     * 故事删除
     * @param storyId
     * @return
     */
    @GetMapping("/admin/delete")
    public R deleteStory(@RequestParam("storyId")Integer storyId){
        String role = ParseJwtUtil.getRole();
        if("1".equals(role)){
            Integer userid = ParseJwtUtil.getUserId();
            List<Integer> oStoryId = adminUserService.findStoryIdByUserId(userid);
            if(oStoryId.contains(storyId)){
                storyService.deleteStory(storyId);
                return R.ok();
            }else {
                return R.error(ResultCode.NO_ROLE_ERROR);
            }
        }else if("0".equals(role)){
            storyService.deleteStory(storyId);
            return R.ok();
        }else {
            return R.error(ResultCode.NO_ROLE_ERROR);
        }
    }

    /**
     * 故事列表
     * @param page
     * @param limit
     * @param storyName
     * @return
     */
    @GetMapping("/admin/list")
    public R getStoryList(@RequestParam("page")Integer page,
                          @RequestParam("limit")Integer limit,
                          @RequestParam(value = "storyName",required = false)String storyName){
        String role = ParseJwtUtil.getRole();
        if("1".equals(role)){
            Integer userid = ParseJwtUtil.getUserId();
            Integer spoId = adminUserService.findSpotIdByUserId(userid);
            Pageable pageable = PageUtil.pagelimit(page, limit);
            if(null == storyName || storyName.trim().length() == 0){
                storyName = null;
            }
            Page<Story> pages = storyService.getListByStoryIds(storyName,pageable,spoId);
            return R.ok().put("list",pages.getContent()).put("totalPages", pages.getTotalPages()).put("count", pages.getTotalElements());
        }else if("0".equals(role)){
            Pageable pageable = PageUtil.pagelimit(page, limit);
            Story story = new Story();
            if(null == storyName || storyName.trim().length() == 0){
                storyName = null;
            }
            story.setStoryName(storyName);
            ExampleMatcher matcher = ExampleMatcher.matching().withMatcher("storyName", ExampleMatcher.GenericPropertyMatchers.contains());
            Example<Story> example = Example.of(story, matcher);

            Page<Story> pages = storyService.getList(example, pageable);
            return R.ok().put("list",pages.getContent()).put("totalPages", pages.getTotalPages()).put("count", pages.getTotalElements());
        }else {
            return R.error(ResultCode.NO_ROLE_ERROR);
        }

    }
}
